﻿/// <reference path="../../javascript/JSintellisense/jquery-1.3.2-vsdoc2.js" />
//套餐相关js

var RowNum = 1;//全局变量。每个商品的id= tbfoodname+foodcount，默认有两个了
var tid = 0;

$(document).ready(function () {
    tid = Request("cityid");
    var searchshopurl = "/ajaxHandler.ashx?method=searchdDvice&t=" + Math.random(1) + "&tid=" + tid;
    $("#tbfoodname1").autocomplete(searchshopurl);

    var id = request("id");
    //编辑时，删除空行
    if (id != "") {
        $("#food_row_1").remove();
        RowNum = $(".hasitem").length;
        $(".fooditem").autocomplete(searchshopurl);
    }

    $(".fooditem").live("keydown", function (e) {
        var ee = e ? e : window.event ? event : null;
        var keyNum = ee.keyCode == 0 ? ee.which : ee.keyCode;
        if (keyNum == 8) {
            //如果是删除键,要删除相关参数;
            $(this).attr("data_name", "");
            $(this).attr("data_id", "0");
            $(this).attr("data_price", "0");
            $(this).attr("data_stock", "0");
            $(this).removeClass("system_shop");

        }
    });


});

foodTable = {
    //添加行
    addrow: function () {
        RowNum++;
        var tr = $("#myfooditem").render({ index: RowNum.toString() });
        $(tr).appendTo("#foodtable");
        var searchshopurl = "/ajaxHandler.ashx?method=searchdDvice&t=" + Math.random(1) + "&tid=" + tid;
        $("#tbfoodname" + RowNum).autocomplete(searchshopurl);
    },
    //删除行
    delRow: function (id) {
        $("#food_row_" + id).remove();
        foodTable.getOldPrice();
    },
    //计算原价
    getOldPrice: function () {
       
    }
};


//提交验证
function checkdata() {
    if (!GetFoodJson()) {
        return false;
    }
   

    if ($(".fooditem").length < 1) {
        alert("请至少添加1个物料");
        return false;
    }
    return j_submitdata("form-list");
}

///返回数据,为空,或者不存在,返回0
function getdata(pid) {
    if (pid == null || pid == undefined || pid == "") {
        pid = "0";
    }
    return pid;
}

//生成商品的json格式數據
function GetFoodJson() {
    var fdStyle = "[";
    for (var i = 0; i < RowNum + 1; i++) {
        if ($("#food_row_" + i).length > 0) {

            var data_id = getdata($("#tbfoodname" + i).attr("data_id"));
            if (data_id == "0" || data_id == "") {
                alert("物料必须下拉选择");
                return false;
            }


            fdStyle += "{'revevar2':'" + $("#tbfoodname" + i).val() + "','revevar1':'" + data_id + "','changestock':'" + getdata($("#tbfoodcount" + i).val()) + "','revefloat1':'" + getdata($("#tbfoodname" + i).attr("data_price")) + "'";
            fdStyle += "},";
        }
    }
    fdStyle = fdStyle.replace(/,$/, "");
    fdStyle += "]";
    document.getElementById("hidStyle").value = fdStyle;

    return true;
}
